<?php
namespace Ygcl\Controller;
use Admin\Controller\AdminbaseController;
class OrderadminController extends AdminbaseController {
	
	protected $m;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('YgclOrder');
	}
	
    public function index(){
    	if(!$_GET['status']){
    		$_GET['status'] = 0;
    	}
    	$this->assign('role',$this->curUser['role']);
    	$this->display();
    }
    
    public function lists(){
    	$lid = I('get.lid','0');
    	$this->lid = $lid;
    	$lessoninfo = D('YgclLesson')->where('id = '.$lid)->relation(true)->find();
    	$this->assign('lessoninfo', $lessoninfo);
    	$this->assign('role',$this->curUser['role']);
    	$this->display();
    }
    
    public function midpay(){
    	if(!$_GET['status']){
    		$_GET['status'] = 1;
    	}
    	$this->assign('role',$this->curUser['role']);
    	$this->display();
    }
    
    public function kjbm(){
    	$this->assign('role',$this->curUser['role']);
    	$this->display();
    }
    
    public function changemidpay(){
    	$m = M('YgclMidpay');
    	$id = I('id');
    	$info = $m->where(array('id'=>$id))->find();
    	if($info['status'] == 1){
    		$data['status'] = 2;//已处理
    	}else{
    		$data['status'] = 1;//未处理
    	}
    	$data['id'] = $id;
    	$data['update_time'] = date('Y-m-d H:i:s');
    	if($m->save($data)){
    		//TODO-积分增加
    		$order = M('YgclOrder')->where(array('id'=>$info['orderid']))->find();
    		if($data['status'] == 2){
    			M('Users')->where(array('uid'=>$info['uid']))->setInc('score',ceil($order['total']));
    			$lsid = A('Home/LessonStudentRelation')->_add_lesson_student_relation($info['orderid']);
    			R('Api/Lessonapi/lesson_number_inc',array('tid'=>$order['termid'],'lid'=>$order['lid']));
    			$w = array('id'=>$id,'lsid'=>$lsid);
    			$m->save($w);
    			//添加学员记录
    			$uid = $this->curUser['uid'];
    			R('Api/Record/addRecord',array('uid'=>$uid,'lsid'=>$lsid,'remark'=>'',$type="midpay"));
    		}else{
    			//积分减少
    			M('Users')->where(array('uid'=>$info['uid']))->setDec('score',ceil($order['total']));
    			A('Home/LessonStudentRelation')->_del_lesson_student_relation($info['lsid']);
    			//班级学期人数减少
    			R('Api/Lessonapi/lesson_number_dec',array('tid'=>$order['termid'],'lid'=>$order['lid']));
    			$w = array('id'=>$id,'lsid'=>'');
    			$m->save($w);
    		}
    		$this->success('处理成功');
    	}else{
    		$this->error('处理失败');
    	}
    }
    
    //订单详情
   	public function detail(){
   		$id = I('get.id',0);
   		$info = $this->m->where('id = '.$id)->relation(true)->find();
   		$info['classtype'] = M('YgclClassType')->where(array('id'=>$info['lesson']['lessontype']))->find();
   		if($this->info['type'] == 'tiyan'){
   			$this->tiyan = D('YgclTiyan')->where(array('id'=>$this->info['objectid']))->find();
   		}
   		if($this->info['type'] == 'qitatiyan'){
   			$this->qitatiyan = D('YgclQitatiyan')->where(array('id'=>$this->info['objectid']))->find();
   		}
   		$this->info = $info;
   		$this->display();
   	} 
    
   	public function detail_post(){
   		if(IS_POST){
   			$_POST['update_time'] = Date('Y-m-d H:i:s');
   			if($this->m->create()){
   				if($this->m->save()){
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
   			}
   		}
   	}
   	
    public function del(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	$info = $this->m->where($map)->find();
    	if($this->m->where($map)->delete()){
    		$this->success('删除成功',U('index'));
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function midpay_del(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	if(M('YgclMidpay')->where($map)->delete()){
    		$this->success('删除成功',U('midpay'));
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    //导出
    public function export(){
    	header("Content-type: text/html; charset=utf-8");
    	$config = array(
    			'fields' => array('订单编号','金额','状态','微信订单号','类型','学生姓名','家长姓名','电话','状态','学期','校区','课堂','班级','班级类型','班级对象','上课时间')
    	);
    	$excel = new \Common\ORG\MiniwebExcel($config);
    	 
    	$mapp = $_GET;
    	unset($mapp['_URL_']);
    	unset($mapp["__hash__"]);
    	$time1 = 0;
    	$time2 = date('Y-m-d H:i:s');
    	foreach($mapp as $k=>$v){
    		if($v){
    			if($k != 'start_time' && $k != 'end_time' && $k != 'p' && $k != 'searchtype' && $k != 'keywords'){
    				$map[$k] = $v;
    			}
    			else{
    				if($k == 'start_time') $time1 = $v;
    				if($k == 'end_time') $time2 = $v;
    			}
    			if($k != 'p'){
    				$path .= $k.'/'.$v.'/';
    			}
    
    		}
    	}
    	if($_GET['searchtype'] && $_GET['keywords']){
    		$map[$_GET['searchtype']] = array('like','%'.$_GET['keywords'].'%');
    	}
    	$map['create_time'] = array('between',array($time1,$time2));
    	 
    	$xiaoqu = R('Apiadmin/getxiaoqurole');
    	if($xiaoqu){
    		$map['did'] = array('in',$xiaoqu);
    	}
    	$map['type'] = array('not in',array('midbm','kjbm','tiyan'));
		

    	$info = D('YgclOrder')->where($map)->relation(true)->order('id desc')->select();

    	$type = C('ORDER_TYPE');
    	$status = array('1'=>'已支付','2'=>'未支付','3'=>'已过期');
    	$stustatus = array('laosheng'=>'老生','xinsheng'=>'新生');

    	foreach ($info as $key => $if){
    		$classtype = M('YgclClassType')->where(array('id'=>$if['lesson']['lessontype']))->find();
    		$data[$key] = array(
    				'0' => $if['ddbh'].' ',
    				'1' => $if['total'],
    				'2' => $status[$if['status']],
    				'3' => $if['wxddh'].' ',
    				'4' => $type[$if['type']],
    				'5' => $if['stuname'],
    				'6' => $if['jzxm'],
    				'7' => $if['tel'].' ',
    				'8' => $stustatus[$if['studentstatus']],
    				'9' => $if['term']['title'],
    				'10' => $if['xiaoqu']['title'],
    				'11' => $if['kecheng']['title'],
    				'12' => $if['lesson']['lessonname'],
    				'13' => $classtype['title'],
    				'14' => $classtype['target1'].'-'.$classtype['target2'],
    				'15' => $if['lesson']['lessontime']
    		);
    	}
    
		$excel->export($data);
		
		//日志
		$uid = session('loginUser.uid');
		$info = '导出订单信息。'.json_encode($map);
		$userslog = new \Userslog\Service\LogService();
		$userslog->addLog($uid,'export',$info);
    }
    
    //导出
    public function export_midpay(){
    	header("Content-type: text/html; charset=utf-8");
    	$config = array(
    			'fields' => array('订单编号','金额','状态','微信订单号','类型','学生姓名','家长姓名','电话','状态','学期','校区','课堂','班级','班级类型','班级对象','上课时间','备注')
    	);
    	$excel = new \Common\ORG\MiniwebExcel($config);
    
    	$mapp = $_GET;
    	unset($mapp['_URL_']);
    	unset($mapp["__hash__"]);
    	$time1 = 0;
    	$time2 = date('Y-m-d H:i:s');
    	foreach($mapp as $k=>$v){
    		if($v){
    			if($k != 'start_time' && $k != 'end_time' && $k != 'p' && $k != 'searchtype' && $k != 'keywords'){
    				$map[$k] = $v;
    			}
    			else{
    				if($k == 'start_time') $time1 = $v;
    				if($k == 'end_time') $time2 = $v;
    			}
    			if($k != 'p'){
    				$path .= $k.'/'.$v.'/';
    			}
    
    		}
    	}
    	if($_GET['searchtype'] && $_GET['keywords']){
    		$map[$_GET['searchtype']] = array('like','%'.$_GET['keywords'].'%');
    	}
    	$map['update_time'] = array('between',array($time1,$time2));
    
    	$xiaoqu = R('Apiadmin/getxiaoqurole');
    	if($xiaoqu){
    		$map['did'] = array('in',$xiaoqu);
    	}
    	 
    	$info = D('YgclMidpay')->where($map)->order('id desc')->select();
    	
    	$type = C('ORDER_TYPE');
    	$status = array('1'=>'已支付','2'=>'未支付','3'=>'已过期');
    	$stustatus = array('laosheng'=>'老生','xinsheng'=>'新生');
    
    	foreach ($info as $key => $if){
    		$ooo =  D('YgclOrder')->where(array('id'=>$if['orderid']))->relation(true)->find();

    		$classtype = M('YgclClassType')->where(array('id'=>$ooo['lesson']['lessontype']))->find();
    		$data[$key] = array(
    				'0' => $ooo['ddbh'].' ',
    				'1' => $ooo['total'],
    				'2' => $status[$ooo['status']],
    				'3' => $ooo['wxddh'].' ',
    				'4' => $type[$ooo['type']],
    				'5' => $ooo['stuname'],
    				'6' => $ooo['jzxm'],
    				'7' => $ooo['tel'].' ',
    				'8' => $stustatus[$ooo['studentstatus']],
    				'9' => $ooo['term']['title'],
    				'10' => $ooo['xiaoqu']['title'],
    				'11' => $ooo['kecheng']['title'],
    				'12' => $ooo['lesson']['lessonname'],
    				'13' => $classtype['title'],
    				'14' => $classtype['target1'].'-'.$classtype['target2'],
    				'15' => $ooo['lesson']['lessontime'],
    				'16' => $ooo['remark']
    		);
    	}
    
		$excel->export($data);
		
		//日志
		$uid = session('loginUser.uid');
		$info = '导出中途报名信息。'.json_encode($map);
		$userslog = new \Userslog\Service\LogService();
		$userslog->addLog($uid,'export',$info);
    }
    
}